#include "sendmail.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

struct upload_status {
    const char *readptr;
    size_t sizeleft;
};

static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp) {
    struct upload_status *upload_ctx = (struct upload_status *)userp;
    size_t buffer_size = size * nmemb;

    if (upload_ctx->sizeleft) {
        size_t copy_size = buffer_size < upload_ctx->sizeleft ? buffer_size : upload_ctx->sizeleft;
        memcpy(ptr, upload_ctx->readptr, copy_size);
        upload_ctx->readptr += copy_size;
        upload_ctx->sizeleft -= copy_size;
        return copy_size;
    }

    return 0;
}

void send_alert_email(const char* message) {
    CURL *curl;
    CURLcode res;
    struct curl_slist *recipients = NULL;

    curl = curl_easy_init();
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_USERNAME, "xxxx@163.com"); // SMTP 服务器的用户名和密码
        curl_easy_setopt(curl, CURLOPT_PASSWORD, "xxx");  
        curl_easy_setopt(curl, CURLOPT_USERNAME, "xxxx@163.com"); // SMTP 服务器的用户名和密码
        curl_easy_setopt(curl, CURLOPT_PASSWORD, "xxxx");  
        curl_easy_setopt(curl, CURLOPT_USERNAME, "xxx@163.com"); // SMTP 服务器的用户名和密码
        curl_easy_setopt(curl, CURLOPT_PASSWORD, "xxxx");  
        curl_easy_setopt(curl, CURLOPT_URL, "smtps://smtp.163.com:587"); //SMTP 服务器的 URL 和端口
        curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);    //启用 SSL/TLS 加密
        curl_easy_setopt(curl, CURLOPT_MAIL_FROM, "<xxxx@163.com>");  //设置发件人地址

        recipients = curl_slist_append(recipients, "<xxx@qq.com>");  //设置收件人地址
        if (recipients == NULL) {
            fprintf(stderr, "curl_slist_append() failed\n");
            curl_easy_cleanup(curl);
            return;
        }
        curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
        printf("send_alert_email: %s\n", message);

        //邮件内容
        char payload_text[1024];
        int payload_length = snprintf(payload_text, sizeof(payload_text), "To: xxx@qq.com\r\n"
                                                                         "From: xxx@163.com\r\n"
                                                                         "Subject: Alert\r\n"
                                                                         "\r\n"
                                                                         "%s\r\n", message);
        if (payload_length >= sizeof(payload_text)) {
            fprintf(stderr, "Payload text is too long\n");
            curl_slist_free_all(recipients);
            curl_easy_cleanup(curl);
            return;
        }

        printf("Payload text: %s\n", payload_text);

        struct upload_status upload_ctx;
        upload_ctx.readptr = payload_text;
        upload_ctx.sizeleft = strlen(payload_text);

        curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
        curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
        curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);

        res = curl_easy_perform(curl);
        printf("curl_easy_perform() returned %d\n", res);
        if(res != CURLE_OK) {
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
        }

        curl_slist_free_all(recipients);
        curl_easy_cleanup(curl);
    } else {
        fprintf(stderr, "curl_easy_init() failed\n");
    }
}
